﻿/*
 * Copyright 2010 Pieter De Rycke
 *
 * This file is part of Frozen Bubble XNA.
 *
 * Frozen Bubble XNA is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * Frozen Bubble XNA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Frozen Bubble XNA. If not, see 
 * <http://www.gnu.org/licenses/>.
 *
 * Artwork:
 *    Alexis Younes <73lab at free.fr>
 *      (everything but the bubbles)
 *    Amaury Amblard-Ladurantie <amaury at linuxfr.org>
 *      (the bubbles)
 *
 * Soundtrack:
 *    Matthias Le Bidan <matthias.le_bidan at caramail.com>
 *      (the three musics and all the sound effects)
 *
 * Design & Programming:
 *    Guillaume Cottenceau <guillaume.cottenceau at free.fr>
 *      (design and manage the project, whole Perl sourcecode)
 *
 * XNA and Windows Phone 7 port:
 *    Pieter De Rycke
 *
 * Websites:
 *    http://www.frozen-bubble.org/
 *    http://pieterderycke.wordpress.com/
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace FrozenBubble
{
    public static class Common
    {
        public static int Even(int number)
        {
            return (number % 2 ==0 ? 1 : 0);
        }

        public static int Odd(int number)
        {
            return (number % 2 == 1 ? 1 : 0);
        }

        public static double Odd(double number)
        {
            return (number % 2 == 1 ? 1.0 : 0.0);
        }

        public static double Sqr(double value)
        {
            return value * value;
        }

        public static XElement ToXElement(this Bubble bubble)
        {
            return ToXElement(bubble, null);
        }

        public static XElement ToXElement(this Bubble bubble, string name)
        {
            XElement bubbleElement = new XElement("bubble");
            if(name != null)
                bubbleElement.Add(new XAttribute("name", name));
            bubbleElement.Add(new XAttribute("number", bubble.Number));
            bubbleElement.Add(new XAttribute("arrayposx", bubble.ArrayPosition.X));
            bubbleElement.Add(new XAttribute("arrayposy", bubble.ArrayPosition.Y));
            bubbleElement.Add(new XAttribute("screenposx", bubble.ScreenPosition.X));
            bubbleElement.Add(new XAttribute("screenposy", bubble.ScreenPosition.Y));
            bubbleElement.Add(new XAttribute("isrootbubble", bubble.IsRootBubble));
            return bubbleElement;
        }

#if WINDOWS_PHONE
        // We have to implement this because this method is not available for the moment on WP7
        public static void RemoveAll<T>(this IList<T> collection, Predicate<T> match)
        {
            if (match == null)
            {
                throw new ArgumentNullException("match");
            }

            for (int i = collection.Count - 1; i >= 0; i--)
            {
                T item = collection[i];
                if (match(item))
                    collection.RemoveAt(i);
            }
        }
#endif
    }
}
